package com.tygz.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.auto.base.pojo.SysBasedata;
import com.auto.base.pojo.SysModule;
import com.base.common.BaseDataElement;
import com.base.common.DataElement;
import com.base.util.JUtil;
import com.tygz.pojo.SysModuleTree;




final public class JtargetUtil {
	
	/**
	 * 生成单个数据元
	 */
	public static Map<Object,Object> getDataBaseElements_(String value){
		//1:身份证;2:护照;3:其他;
		String[] arr1 = value.split(";");
		//  Map<Object,Object> map1 = new HashMap <Object,Object>();
		Map<Object,Object> map2 = new HashMap <Object,Object>();
		for(String s:arr1){
			if(JUtil.isNull(s)){
				continue;
			}
			String[] arr2 = s.split(":");
			if(null == arr2 || arr2.length<2){
				continue;
			}
			map2.put(arr2[0], arr2[1]);
		}
		return map2;
	}

	
	/**
	 * 生成单个数据元集合
	 */
	public static  Map<Object,Object> getDataBaseElementsList_(List<SysBasedata> listBasedata){
		Map<Object,Object> map1 = new HashMap <Object,Object>();
		if(listBasedata.isEmpty()){
			return map1;
		}
		for(SysBasedata sb:listBasedata){
			map1.put(sb.getBaseCode(),getDataBaseElements_(sb.getBaseDatalist()));
		}
		return map1;
	}

	/**
	 * 生成单个数据元
	 */
	public static BaseDataElement getDataBaseElements(String code,String value){
		//1:身份证;2:护照;3:其他;
		String[] arr1 = value.split(";");
		List<DataElement> list = new ArrayList<DataElement>();
		for(String s:arr1){
			if(JUtil.isNull(s)){
				continue;
			}
			String[] arr2 = s.split(":");
			DataElement  de = new DataElement();
			de.setName(arr2[0]);
			de.setValue(arr2[1]);
			list.add(de);
		}
		BaseDataElement bde = new BaseDataElement();
		bde.setDataElements(list);
		bde.setTypeCode(code);
		return bde;
	}

	/**
	 * 生成单个数据元集合
	 */
	public static List<BaseDataElement> getDataBaseElementsList(List<SysBasedata> listBasedata){
		List<BaseDataElement>  list = new ArrayList<BaseDataElement> ();
		if(listBasedata.isEmpty()){
			return list;
		}
		for(SysBasedata sb:listBasedata){
			BaseDataElement bde = getDataBaseElements(sb.getBaseCode(),sb.getBaseDatalist());
			list.add(bde);
		}
		return list;
	}

	/**
	 * 
	 * @param deptTree
	 * @param deptList
	 */
	public static List<SysModuleTree> getModuleTreegridData(List<SysModule> moduleList,int start) {
		List<SysModuleTree> treeList = new ArrayList<SysModuleTree>();
		for(SysModule c : moduleList) {
			if(c.getModulePid() == start) {
				SysModuleTree ctree = module2Tree(c);
				treeList.add(ctree);
				findModuleChildren(ctree,moduleList);
			}
		}
		return treeList;
	}
	
	
	/**
	 * 
	 * @param SysModuleTree
	 * @param moduleList
	 */
	public static void findModuleChildren(SysModuleTree tree,List<SysModule> moduleList) {
		Integer id = tree.getId();
		for(SysModule c : moduleList) {
			Integer pid = c.getModulePid();
			if(pid==id) {
				SysModuleTree childTree = module2Tree(c);
				List<SysModuleTree> treeList =  tree.getChild();
				if(treeList == null) {
					treeList = new ArrayList<SysModuleTree>();
					tree.setChild(treeList);
				}
				treeList.add(childTree);
				//继续查找子节点
				findModuleChildren(childTree, moduleList);
			}
		}
	}

	/**
	 * 
	 * @param  module
	 * @return
	 */
	public static SysModuleTree module2Tree(SysModule module) {
		SysModuleTree tree = new SysModuleTree();
		tree.setId(module.getId());
		tree.setHref(module.getModuleUrl());
		tree.setIcon(module.getModuleIcon());
		tree.setTarget(module.getModuleTarget());
		tree.setTitle(module.getModuleName());
		tree.setSort(module.getSort());
		//默认设置树不展开
		tree.setState("closed");
		return tree;
	}

	/**
	 * 
	 * @param homeViewUrl
	 * 得到首页的配置信息
	 * @return
	 */
	public static SysModuleTree getHomeViewSysModuleTree(String homeViewUrl) {
		SysModuleTree tree = new SysModuleTree();
		tree.setId(-1);
		tree.setHref(homeViewUrl);
		tree.setIcon("icon01.png");
		tree.setTarget("rightFrame");
		tree.setTitle("我的桌面");
		tree.setSort(-1);
		//默认设置树不展开
		tree.setState("closed");
		return tree;
	}

	public static Map<String,String>  getSysBasedataMapByKey(List<SysBasedata> list,String key){
		Map<String,Map<String,String>>  listMap = sysBasedataToMap(list);
		return listMap.get(key);
	}

	public static Map<String,Map<String,String>>  sysBasedataToMap(List<SysBasedata> list){
		Map<String,Map<String,String>>  map = new HashMap<String,Map<String,String>>();
		if(null == list || list.isEmpty()){
			return map;
		}
		for(SysBasedata record:list){
			String value = record.getBaseDatalist(),key = record.getBaseCode();
			map.put(key, strToMap(value));
		}
		return map;
	}

	public static Map<String,String> strToMap(String value){
		Map<String,String> map = new HashMap<String,String>();
		if(JUtil.isNull(value)){
			return map;
		}
		String[] arr = value.split(";");
		if(arr.length ==0){
			return map;
		}
		for(String v:arr){
			String[] arrV = v.split(":");
			if(arrV.length<=1){
				continue;
			}
			map.put(arrV[0], arrV[1]);
		}
		return map;
	}

}
